package com.lingxu.base.common.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Title: HandlerUtil
 * @Package: com.lingxu.config
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2022/9/6 17:26
 * @Description:
 * @Version: 1.0.0
 */
public class HandlerUtil {

    private static Logger logger = LoggerFactory.getLogger(HandlerUtil.class);

    /**
     * @Author ysj闫世杰
     * @Description 处理日期格式化
     * @Date 2022/9/6 17:28
     * @Param: date
     * @return java.util.Date
     **/
    public static Date getDate(String date){
        try {
            if(!StringUtils.isEmpty(date)){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
                if(date.length() > 10){
                    date = getIndexOf(date);
                    String index = date.substring(3,7) + "-" + date.substring(0,2);
                    return format.parse(index);
                }else {
                    return format.parse(date);
                }
            }
            return null;
        }catch (Exception e){
            logger.error("操作异常",e);
        }
        return null;
    }
    private static String getIndexOf(String a){
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(a);
        return m.replaceAll("").trim();
    }


    public String getBirthFromIdCard(String idCard) {
        if(!StringUtils.isEmpty(idCard)){
            if (idCard.length() != 18 && idCard.length() != 15) {
                return "请输入正确的身份证号码";
            }
            String year = idCard.substring(6).substring(0, 4);// 得到年份
            String month = idCard.substring(10).substring(0, 2);// 得到月份
            String day = idCard.substring(12).substring(0, 2);// 得到日
            return (year + "-" + month + "-" + day);
        }
        return null;
    }

    /**
     * 获取出生日期
     *
     * @return 返回日期格式
     */
    public static Date getBirthDayFromIdCard(String idCard) throws ParseException {
        Date birth = null;
        if(StringUtils.isNotEmpty(idCard)){
            String year = idCard.substring(6).substring(0, 4);// 得到年份
            String month = idCard.substring(10).substring(0, 2);// 得到月份
            String day = idCard.substring(12).substring(0, 2);// 得到日
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            birth = format.parse(year + "-" + month + "-" + day);
        }
        return birth;
    }

    /**
     * 获取性别
     * @return int
     */
    public static String getSexFromIdCard(String idCard) {
        String sex = null;
        if(StringUtils.isNotEmpty(idCard)){
            if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
                sex = "0"; // 女
            } else {
                sex = "1"; // 男
            }
        }
        return sex;
    }

    /**
     * 转换文件大小
     */
    public static String formatFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        return fileSizeString;
    }
}
